<?php


namespace app\api\traits;

use think\facade\Log;

/**
 * Trait CommonTrait
 * @package app\api\traits
 * @desc 通用方法
 */
trait CommonTrait
{
    /**
     * @desc 统一下发数组数据
     * @param int $code 状态码,400:失败,200:成功
     * @param string $msg 提示信息
     * @param array $data 数据
     */
    public static function returnArr($code = 400, $msg = '', $data = [])
    {
        if (empty($msg)) {
            $msg = self::getStatus($code);
        }
        return compact('code', 'msg', 'data');
    }

    /**
     * @desc 统一下发json数据
     * @param int $code 状态码,400:失败,200:成功
     * @param string $msg 提示信息
     * @param array $data 数据
     */
    public static function returnJson($code = 400, $msg = '', $data = [])
    {
        return json(self::returnArr($code, $msg, $data));
    }

    /**
     * @desc 获取status状态码对应信息
     * @param $code
     * @return mixed|string
     */
    public static function getStatus($code)
    {
        $msgStr = config("status." . $code);
        return $msgStr == '' ? '' : $msgStr;
    }

    /**
     * @desc 增加日志记录
     * @param $code
     * @return mixed|string
     */
    public static function writeLog($msg = '', $path = '')
    {
        // 指定路径写入日志
        if ($path) {
            $filePath = runtime_path() . $path . date('Y-m-d') . '.log';// 文件路径
            file_put_contents($filePath, $msg . PHP_EOL, FILE_APPEND);
        } else {
            Log::write($msg);
        }
    }

    /**
     * @desc curl get方法
     * @param string $url
     * @param array $options
     * @param array $header 设置header头
     * @return mixed
     */
    public static function curlGet($url = '', $options = [], $header = [])
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // 设置HTTP头
        }
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    /**
     * @desc curl post方法
     * @param string $url
     * @param array $data 数据
     * @return mixed
     */
    public static function curlPost($url = '', $data = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    /**
     * @desc 添加请求日志
     * @param string $logId 日志id
     * @param string $url 地址
     * @param array $param 参数
     */
    public static function addRequestLog($logId, $url = '', $param = [])
    {
        $msg = sprintf('日志id:%s,请求地址:%s,请求参数:%s', $logId, $url,
            json_encode($param, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
        $res = self::writeLog($msg);
    }

    /**
     * @desc 添加下发日志
     * @param string $logId 日志id
     * @param array $param 数据
     */
    public static function addReturnLog($logId, $param = [])
    {
        $msg = sprintf('日志id:%s,下发数据:%s', $logId,
            json_encode($param, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
        $res = self::writeLog($msg);
    }

    /**
     * @desc 输出json数据并退出
     */
    public static function returnJsonExit($ret)
    {
        header('Content-Type:text/json;charset=utf-8');
        echo json_encode($ret, JSON_UNESCAPED_UNICODE);
        die;
    }
}